package com.his.struts.action;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.shebei.SheBeiDao;
import com.his.struts.dao.shebei.SheBeiImp;
import com.his.struts.form.ShebeiForm;
import com.his.struts.util.PageUtil;

/**
 * 设备功能类
 * 
 * @author liumiaoyue
 * 
 */
public class ShebeiAction extends DispatchAction {
	SheBeiDao dao = new SheBeiImp();
	Logger log = Logger.getLogger(ShebeiAction.class);

	/**
	 * Method getSb 得到所有设备
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getSb(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		String admin = request.getParameter("admin");
		int tiaoshu = 4;
		if("lmy".equals(admin)){
			tiaoshu = 10;
		}
		// 判断设备id是否为空
		String id = request.getParameter("sbid");
		if (id == null) {
			id = "";
		} else {
			int sbId = Integer.parseInt(id);
			dao.delShebei(sbId);
			request.setAttribute("message", "删除成功");
		}

		String sql = "shebei order by sbId desc";
		// 调用分页类
		ResultSet rs = PageUtil.getPage(sql, tiaoshu, request);
		if (rs != null) {
			// 封装
			List<ShebeiForm> list = dao.getShebeiBean(rs);
			// 数据放到request中
			request.setAttribute("sbList", list);
		}
		if("lmy".equals(admin)){
			return mapping.findForward("allsb");
		}
		return mapping.findForward("sbList");
	}

	/**
	 * Method getSb 添加设备
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward addSb(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 设备form
		ShebeiForm shebeiForm = (ShebeiForm) form;
		// 得到设备图片的名称并设置图片
		String fileName = shebeiForm.getPic().getFileName();
		shebeiForm.setSbPic(fileName);
		// 得到图片路径
		String path = request.getRealPath("/images/shebei/" + fileName);
		try {
			// 读写并上传
			InputStream input = shebeiForm.getPic().getInputStream();
			OutputStream output = new FileOutputStream(path);
			IOUtils.copy(input, output);
			input.close();
			output.close();
		} catch (FileNotFoundException e) {
			log.error(e.getMessage());
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		// 添加设备并得到所有
		dao.addShebei(shebeiForm);

		String sql = "shebei order by sbId desc";
		// 调用分页类
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if (rs != null) {
			// 封装
			List<ShebeiForm> list = dao.getShebeiBean(rs);
			// 数据放到request中
			request.setAttribute("sbList", list);
		}
		request.setAttribute("message", "添加成功");
		return mapping.findForward("allsb");
	}

	/**
	 * Method updateSb 更新设备
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward updateSb(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// 上传文件类型
		String type[] = { ".jpg", ".gif", ".png", ".jpeg", ".bmp" };
		// 设备form
		ShebeiForm shebeiForm = (ShebeiForm) form;
		// 得到设备图片的名称并设置图片
		String fileName = shebeiForm.getPic().getFileName();
		// 比较上传图片类型是否符合
		for (String str : type) {
			if (fileName.endsWith(str)) {
				// 设置
				shebeiForm.setSbPic(fileName);
				// 得到图片路径
				String path = request.getRealPath("/images/shebei/" + fileName);
				try {
					// 读写并上传
					InputStream input = shebeiForm.getPic().getInputStream();
					OutputStream output = new FileOutputStream(path);
					IOUtils.copy(input, output);
					input.close();
					output.close();
				} catch (FileNotFoundException e) {
					log.error(e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}
			}
			break;
		}
		// 更新设备
		dao.updateShebei(shebeiForm);
		String sql = "shebei order by sbId desc";
		// 调用分页类
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if (rs != null) {
			// 封装
			List<ShebeiForm> list = dao.getShebeiBean(rs);
			// 数据放到request中
			request.setAttribute("sbList", list);
		}
		request.setAttribute("message", "添加成功");
		return mapping.findForward("allsb");
	}
}